/*
题目链接：https://leetcode.cn/problems/find-all-possible-stable-binary-arrays-i/description/
	刘沛民	2024-12-3
*/

class Solution {
public:
    int numberOfStableArrays(int zero, int one, int limit) {
        int mod = 1'000'000'007;
        vector<vector<vector<int>>> memo(2, vector<vector<int>>(zero + 1, vector<int>(one + 1, -1)));
        auto dfs = [&](auto&& dfs, int ind, int zero, int one) -> int {
            int& res = memo[ind][zero][one];
            if (res != -1) {
                return res;
            }
            if (zero == 0 && one == 0) {
                res = 1;
                return res;
            }
            res = 0;
            if (ind == 0) {
                for (int z = 1; z <= min(zero, limit); z++) {
                    res += (dfs(dfs, 1, zero - z, one) % mod);
                    res %= mod;
                }
            } else if (ind == 1) {
                for (int o = 1; o <= min(one, limit); o++) {
                    res += (dfs(dfs, 0, zero, one - o) % mod);
                    res %= mod;
                }
            }
            return res;
        };
        return (dfs(dfs, 0, zero, one) + dfs(dfs, 1, zero, one)) % mod;
    }
};
